var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var do_DataCache = sm("do_DataCache");
var deviceone = require("deviceone");
var communityUrl = require("url/community");
var communityMessageUrl = communityUrl.url.message.detail;
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var userShieldHelper = require("helper/userShield");
var shareHelper = require("helper/share");
var userUtil = require("helper/user");
var rootview = ui("$");

var pubUrl = require("url/pub");
var bizzType = require("helper/bizzType");
var openHelper = require("helper/open");

//UI
var alayoutRoot = ui("alayoutRoot");
var headerAout = ui("headerAout");
var listViewDetail = ui("listViewDetail");
var id;
var pageNum = 1;//当前页码
var isLastPage = false;//是否最后一页
var isPraised = 2;
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var isAddCommentPraise=false;
var updateData;
//andriod返回键
do_Page.on("back", function() {
	do_App.closePage({isAddCommentPraise:isAddCommentPraise,updateData:updateData});
});
//从open的上层页面返回,刷新数据
do_Page.on("result", function(data){
	if (data.comment_add) {
		pageLoad();
	}
});

var menus=[{"template":0,"menuIcon":"source://image/menu/report.png","menuName":"举报"},
           {"template":0,"menuIcon":"source://image/menu/shield.png","menuName":"屏蔽此人"}];
/****************处理页面-头部-begin*****************/
//左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
//右上角-
headerLeft.on("touch", function() {
	do_App.closePage({isAddCommentPraise:isAddCommentPraise,updateData:updateData});
});

var headerRight = ui(addheader + ".headerRight");
//右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "详情"
})
header.refreshData();
/****************处理页面-头部-end*****************/

//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	//读取当前页面的传入参数
	var data=do_Page.getData();
	id=data.id;
	pageLoad();
});

do_Page.on("sendTextMsg", function(data, e) {
	isAddCommentPraise=true;
	pageLoad();
});

var messageDetailList = mm("do_ListData");
listViewDetail.bindItems(messageDetailList);
var shieldUserId;
var userList4Praise;
function pageLoad(){
	var params = {
		id : id
	};
	qqHttp.post(communityMessageUrl, params, function(data) {
		// 去掉遮盖
		listViewDetail.rebound();
		messageDetailList.removeAll();
		updateData=data.detailVo;//增加评论、点赞回去的data
		data.detailVo.template = 1;//指定模板,加载话题说情
		shieldUserId=data.detailVo.creatorId;
		userList4Praise=data.detailVo.userList4Praise;
		messageDetailList.addOne(data.detailVo);
		listViewDetail.refreshItems();
		bindCommentData();
		var param ={
				bizzId:data.detailVo.id,	
				bizzType:bizzType.Level2.COMMUNITY_MESSAGE,
				isPraised:data.detailVo.isPraised,
				shareTitle:"社区大喇叭",
				shareContent:data.detailVo.content,
				shareId:data.detailVo.id,
				shareType:shareHelper.type.COMMUNIty_MESSAGE
			};
		if(data.detailVo.imgItems.length>0){
			param.shareImage=data.detailVo.imgItems[0].source;
		}
		do_Page.fire("initPraise", param);
	});
	
}

//全部刷新数据
function bindCommentData(){
	pageNum=1;
	var params = {
			bizzId:do_Page.getData().id,
			bizzType:bizzType.Level2.COMMUNITY_MESSAGE,
			page:pageNum	
	};
	httpSilence.post(pubUrl.url.comment.list,params,function(data){
		if(data.commentPage){
			isLastPage = data.commentPage.last;//是否最后一页
			if(messageDetailList.getCount()>1){
				//清除已经加载了话题,把话题列表数据清除
				messageDetailList.removeRange(1, messageDetailList.getCount()-1);
			}
			messageDetailList.addData(data.commentPage.content);
		}
		listViewDetail.refreshItems();//刷新item数据
		do_Page.fire("showAllPraise",{userList4Praise:userList4Praise});
	});
}
do_Page.on("refreshList",function(data){
	listViewDetail.refreshItems();
})
//下一页的评论信息
function getNextPageCommentData(){
	if(!isLastPage){
		pageNum++;
		var params = {
				bizzId:do_Page.getData().id,
				bizzType:bizzType.Level2.COMMUNITY_MESSAGE,
				page:pageNum	
		};
		qqHttp.post(pubUrl.url.comment.list,params,function(data){
			if(data.commentPage){
				isLastPage = data.commentPage.last;//是否最后一页
				messageDetailList.addData(data.commentPage.content);
				listViewDetail.refreshItems();
			}
		});
	}
}

//下拉列表，刷新数据
listViewDetail.on("pull", function(data){
	if (data.state == 2){
		pageLoad();
		listViewDetail.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listViewDetail.on("push", function(data){
	if (data.state == 2){
		getNextPageCommentData();
		listViewDetail.rebound();//复位【一定要做】
	}
});

var footerAout = ui("footerAout");
var addfooter = footerAout.add("footer", "source://view/public/comment/footer.ui", 0, 0);
/*
 * 菜单弹出
 */
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menul.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});
popmenuf.on("popmenuItem", function(data) {
	var msgId = do_Page.getData().id
	switch (parseInt(data)) {
	case 0:
		popmenuf.fire("popmenuClose");
		openHelper.open("source://view/public/report/main.ui",{"bizzId":msgId,"bizzType":bizzType.Level2.COMMUNITY_MESSAGE});
		break;
	case 1:
		popmenuf.fire("popmenuClose");
		userShieldHelper.addShield(shieldUserId,userShieldHelper.type.COMMUNITY_MESSAGE);
		break;
	}
});
var addShare = rootview.add("share","source://view/reuse/share.ui", 0, 0);

//点赞后更新UI
do_Page.on("praise2UpdateCommunityMessage", function(data, e) {
	isAddCommentPraise=true;
	if(messageDetailList.getCount()>0){
		var oneData = messageDetailList.getOne(0);
		oneData.userList4Praise = data.userList4Praise;//点赞人列表
		oneData.praiseUserList = data.praiseUserList;//点赞人列表string
		isPraisedByMe(data.userList4Praise);//判断我是否已点赞
		if(data.userList4Praise!=null && data.userList4Praise.length>0){
			//oneData.isShowPraiseList = true;
			praiseCount = data.userList4Praise.length;//点赞数量
		}else{
			//oneData.isShowPraiseList = false;
			praiseCount = 0;//点赞数量
		}
		oneData.praiseCount=praiseCount;
		oneData.isPraised=isPraised;
		updateData=oneData;//增加评论、点赞回去的data
		messageDetailList.updateOne(0, oneData);//更新“点赞人”
		userList4Praise=data.userList4Praise;
		listViewDetail.refreshItems();
		do_Page.fire("showAllPraise",{userList4Praise:userList4Praise});
	}
	//isUpdateInfo = true;
});
//判断我是否已点赞
function isPraisedByMe(userList){
	if(userUtil.loginToken()==""){
		isPraised = 2;
		return;
	}
	if(userList == null || userList.length <=0){
		isPraised = 2;
		return;
	}
	for(var i in userList){
		if(userList[i].id == userUtil.userId()){
			isPraised = 1;
			break;
		}
	}
}